import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入全局样式表
import '../assets/css/global.css'

Vue.use(VueRouter)

const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../views/Login.vue')
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../views/Home.vue')
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../views/Welcome.vue')
const Users = () => import(/* webpackChunkName:"users" */ '../components/user/Users.vue')
const Roles = () => import(/* webpackChunkName:"prower" */ '../components/power/Roles.vue')
const Rights = () => import(/* webpackChunkName:"prower" */ '../components/power/Rights.vue')
const Cate = () => import(/* webpackChunkName:"goods" */ '../components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName:"goods" */ '../components/goods/Params.vue')
const GoodsList = () => import(/* webpackChunkName:"goods" */ '../components/goods/List.vue')
const Add = () => import(/* webpackChunkName:"goods" */ '../components/goods/Add.vue')
const Order = () => import(/* webpackChunkName:"orders" */ '../components/order/Order.vue')
const Report = () => import(/* webpackChunkName:"report" */ '../components/report/Report.vue')

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/welcome',
    component: Home,
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: Welcome
      },
      {
        path: '/users',
        name: 'Users',
        component: Users,
        meta: {
          bread: ['用户管理', '用户列表']
        }
      },
      {
        path: '/roles',
        name: 'Roles',
        component: Roles,
        meta: {
          bread: ['权限管理', '角色列表']
        }
      },
      {
        path: '/rights',
        name: 'Rights',
        component: Rights,
        meta: {
          bread: ['权限管理', '权限列表']
        }
      },
      {
        path: '/params',
        name: 'Params',
        component: Params,
        meta: {
          bread: ['商品管理', '参数列表']
        }
      },
      {
        path: '/categories',
        name: 'Categories',
        component: Cate,
        meta: {
          bread: ['商品管理', '商品分类']
        }
      },
      {
        path: '/goods',
        name: 'Goods',
        component: GoodsList,
        meta: {
          bread: ['商品管理', '商品列表']
        }
      },
      {
        path: '/add',
        name: 'Add',
        component: Add,
        meta: {
          bread: ['商品管理', '添加商品']
        }
      },
      {
        path: '/orders',
        name: 'Orders',
        component: Order,
        meta: {
          bread: ['订单管理', '订单列表']
        }
      },
      {
        path: '/reports',
        name: 'Reports',
        component: Report,
        meta: {
          bread: ['数据统计', '数据报表']
        }
      }

    ]
  }

]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
